#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) 
{
//     int * p = (int *)malloc((nums1Size + nums2Size)* sizeof(int)) ;
     int *p = NULL; 
     double ret;
     double middle =  (double)(nums1Size + nums2Size) / 2;
     bool badd = false;
     int pos, where = 0;
     int nums1pos = 0, nums2pos = 0;
     if ( (int)(middle - 0.5) < (int)middle)
     {
        badd = true;
        middle -= 1;
     }
     while(nums1pos != nums1Size || nums2pos != nums2Size)
     {

         if (nums1pos != nums1Size  && nums2pos != nums2Size)
         {
             if (*nums1 < *nums2)
             {
                 p = nums1;
                 nums1++;
                 nums1pos++;
             }
             else
             {
                 p = nums2;
                 nums2++;
                 nums2pos++;
             }
         }
         else if (nums1pos == nums1Size)
         {
            p = nums2;
            nums2++;
            nums2pos++;
         }
         else if (nums2pos == nums2Size)
         {
             p = nums1;
             nums1++;
             nums1pos++;
         }
         if (where == (int)middle)
         {
             ret = *p;
             if (badd)
             {
                 if (nums1pos != nums1Size  && nums2pos != nums2Size)
                 {
                     if (*nums1 < *nums2)
                         ret += *nums1;
                     else
                         ret += *nums2;
                 }
                 else if (nums1pos == nums1Size)
                 {
                     ret += *nums2;
                 }
                 else if (nums2pos == nums2Size)
                 {
                     ret += *nums1;
                 }

                 return ret/2;
             }   
             else
                 return *p;
         }
         where++;
         p++;
     }
     return 0;

}

int main()
{
    int s1[2] = {1,2};
    int s2[2] = {3,4};
    cout << findMedianSortedArrays(s1, 2, s2 , 2) << endl;


    int d1[1] = {1};
    int d2[2] = {3,4};
    cout << findMedianSortedArrays(d1, 1, d2 , 2) << endl;
    return 0;
}
